package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.OrderQueryPageBean;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Order;
import cn.itcast.service.OrderListService;


import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Reference;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;



/**
 * @ClassName OrderListController
 * @Description TODO
 * @Author lc
 * @Date 2021/11/16 18:34
 * @Version 1.0
 */
@RestController
@RequestMapping(path = "/order")
@Slf4j
@CrossOrigin
public class OrderListController {

    @Reference(check = false)
    private OrderListService orderListService;

    /**
     * 分页查询预约信息
     * @param queryPageBean
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(OrderQueryPageBean queryPageBean) {
        try {
            PageResult pageResult = orderListService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, pageResult);
        } catch (Exception e) {
            log.error("预约查询失败e:{}",e);
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**
     * 新增和修改 预约信息
     * @param order
     * @return
     */
    @PostMapping("/submit")
    public Result submitOrder(@RequestBody Order order) {
        try {
            log.info("新增或修改预约信息详情order:{}",order);
            long orderTime = order.getOrderDate().getTime();
            long currentTimeMillis = System.currentTimeMillis();
            //判断预约时间是否通过
            if (orderTime < currentTimeMillis) {
                return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
            }
            //判断预约人数是否已满
            Boolean isNotReservations = orderListService.isNotReservations(DateUtils.parseDate2String(order.getOrderDate()));
            if (isNotReservations) {
                return new Result(false, MessageConstant.ORDER_FULL);
            }
            if (null != order.getId()) {
                orderListService.updateOrder(order);
            } else {
                Boolean isExist = orderListService.isExist(order);
                if (isExist){
                    return new Result(false, MessageConstant.HAS_ORDERED);
                }
                orderListService.saveOrder(order);
            }
            return new Result(true, MessageConstant.ORDER_SUCCESS);
        } catch (Exception e) {
            log.error("新增或修改预约失败异常信息:{}",e);
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDER_FAIL);
        }
    }

    /**
     * 查询信息 回显
     * @param id
     * @return
     */
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable("id") String id) {
        try {
            log.info("查询订单信息id:{}",id);
            Order byId = orderListService.findById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, byId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }
}
